2 Android Cihaz Wifi Üzerinden Sesli İletişim
19.01.2016 - 02:15
Merhaba arkadaşlar 2 android cihazı aynı ağa baglı wifi üzerinden 1 i client 1 server olmak üzere clientten server a ses gönderimi yapmak istiyorum ancak io exception da hata alıyorum kodlar :
client-------------------------------------------------------------
DatagramSocket socket = new DatagramSocket();
Log.d("VR", "Socket Created");
durum3.setText("Socket Created");
byte[] buffer = new byte[minBufSize];
Log.d("VR", "Buffer created of size : "+minBufSize);
durum4.setText("Buffer created of size " + minBufSize);
DatagramPacket packet;
final InetAddress destination = InetAddress.getByName(ip.getText().toString());
Log.d("VR", "Adress Retrieved");
durum5.setText("Adress retrieved");
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize);
Log.d("VR", "Recorder initialized");
durum.setText("Recorder initialized");
recorder.startRecording();
while(status == true) {
//reading data from MIC into buffer
minBufSize = recorder.read(buffer, 0, buffer.length);
//putting buffer in the packet
packet = new DatagramPacket (buffer,buffer.length,destination,port);
socket.send(packet);
durum.setText("MinBufferSize : " + minBufSize);
}
} catch(UnknownHostException e) {
Log.d("VR", "UnkownHostException");
durum.setText("UnkownHostException");
} catch (IOException e) {
Log.d("VR", "İOException");
durum.setText("IOException");
}
server------------------------
DatagramSocket socket = new DatagramSocket();
durum.setText("Socket Created");
byte[] buffer = new byte[256];
//minimum buffer size. need to be careful. might cause problems. try setting manually if any problems faced
int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
speaker = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,channelConfig,audioFormat,minBufSize,AudioTrack.MODE_STREAM);
speaker.play();
while(status == true) {
try {
DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
socket.receive(packet);
Log.d("VR", "Packet Received");
durum.setText("Packet Received");
//reading content from packet
buffer=packet.getData();
Log.d("VR", "Packet data read into buffer");
durum.setText("Packet data read into buffer");
//sending data to the Audiotrack obj i.e. speaker
speaker.write(buffer, 0, minBufSize);
Log.d("VR", "Writing buffer content to speaker");
durum.setText("Writing buffer content to speaker");
} catch(IOException e) {
Log.e("VR","IOException");
durum.setText("IOException");
}
}
} catch (Exception e) {
Log.e("VR", "SocketException");
durum.setText("Socket Exception");
}
10
Görüntülenme
0 Beğeni